<template>
  <div>
    <el-form ref="pram" :model="pram" :rules="rules" label-width="100px" @submit.native.prevent>
      <el-form-item label="职称名称" prop="name">
        <el-input v-model.trim="pram.name" maxlength="50" placeholder="请输入职称名称" clearable/>
      </el-form-item>
      <el-form-item label="职称系列" prop="series_id">
        <el-select v-model="pram.series_id" placeholder="请选择职称系列">
          <el-option
            v-for="item in seriesList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="职称级别" prop="level_id">
        <el-select v-model="pram.level_id" placeholder="请选择职称级别">
          <el-option
            v-for="item in levelList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态">
        <el-switch v-model="pram.status" :active-value="1" :inactive-value="0"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handlerSubmit('pram')">{{ isCreate === 0 ? '确定' : '更新' }}</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {Debounce} from '@/utils/validate'
import {getEditData} from "@/utils/utils";
import {saveTitles} from "@/api/titles";

export default {
  // name: "edit"
  props: {
    isCreate: {
      type: Number,
      required: true
    },
    editData: {
      type: Object,
      default: () => {
        return {rules: []}
      }
    },
    series: {
      type: Array
    },
    level: {
      type: Array
    },
  },
  data() {
    return {
      constants: this.$constants,
      pram: {
        name:'',
        series_id:null,
        level_id:null,
        status: 1,
        id: 0,
      },
      seriesList: [],
      levelList: [],
      rules: {
        name: [{required: true, message: '请输入机构名称', trigger: ['blur', 'change']}],
        series_id: [{required: true, message: '请选择职称系列', trigger: ['blur', 'change']}],
        level_id: [{required: true, message: '请选择职称级别', trigger: ['blur', 'change']}],
      },
    }
  },
  mounted() {
    this.initEditData()
  },
  methods: {
    close() {
      this.$emit('hideEditDialog')
    },
    initEditData() {
      this.seriesList = this.series
      this.levelList = this.level

      if (this.isCreate !== 1) return //编辑
      this.pram =
        {
          ...getEditData(Object.keys(this.pram), this.editData)
        };
    },
    handlerSubmit: Debounce(function (form) {
      this.$refs[form].validate(valid => {
        if (!valid) return
        saveTitles(this.pram).then(data => {
          this.$message.success('保存成功')
          this.$emit('hideEditDialog')
        })
      })
    }),
  }
}
</script>

<style scoped>

</style>
